package com.unsiv.torch;

import java.util.List;

import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;

/**
 * <p>
 * camara help object
 * </p>
 * 
 * @author fanzhen
 * @time 10:48:35 AM Oct 8, 2011
 * @email fanzhen.meng@gmail.com
 */
public final class MyCamaraHelper {
	private static final Camera camera = Camera.open();

	// does your device support flash mode?
	public static boolean checkCamera() {
		boolean isSuport = false;
		if (camera != null) {
			List<String> supportedFlashModes = camera.getParameters()
					.getSupportedFlashModes();
			isSuport = supportedFlashModes != null
					&& supportedFlashModes
							.contains(Parameters.FLASH_MODE_TORCH);
		}
		return isSuport;
	}

	// does your phone have a camera?
	public static boolean hasCamera(Context context) {
		PackageManager pm = context.getPackageManager();
		if (pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
			return true;
		} else {
			return false;
		}
	}

	// turn on your camera
	public static void turnOn() {
		Parameters parameters = camera.getParameters();
		parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
		camera.setParameters(parameters);
	}

	// turn off your camera
	public static void turnOff() {
		if (camera != null){
			Parameters parameters = camera.getParameters();
			parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
			camera.setParameters(parameters);
		}
	}

	// turn your camera off or on
	public static void switchCameraFlashMode() {
		Parameters parameters = camera.getParameters();
		if (Parameters.FLASH_MODE_OFF.equals(parameters.getFlashMode())) {
			parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
		} else {
			parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
		}
		camera.setParameters(parameters);
	}
	// turn your camera off or on
	public static boolean isCameraFlashMode() {
		Parameters parameters = camera.getParameters();
		if (Parameters.FLASH_MODE_TORCH.equals(parameters.getFlashMode())) {
			return true;
		} else {
			return false;
		}
	}
	//release camera
	public static void _release(){
		if (null != camera) {
			camera.release();
		}
	}
}
